package com.ti.shangou.controller;

import com.ti.shangou.pojo.dto.ResponseDTO;
import com.ti.shangou.pojo.vo.UserVO;
import com.ti.shangou.service.impl.PermissionServiceImpl;
import com.ti.shangou.service.impl.UserServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController extends BaseController {
    @Resource
    UserServiceImpl userService;
    @Resource
    PermissionServiceImpl permissionService;

    @ResponseBody
    @RequestMapping("/login")
    ResponseDTO login(UserVO userVO) {// 这个方法是执行登录操作的
        // 获取subject
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(userVO.getPhone(), userVO.getPassword());
        getSession().setAttribute("code",userVO.getCode());
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);// 只要 执行login方法，那么它就会跑到userRealm里边的认证方法（doGetAuthenticationInfo）
        } catch (AuthenticationException a) {
            a.printStackTrace();
            return ResponseDTO.fail("登录失败");
        }
        return ResponseDTO.ok("登录成功");
    }

    // 这个方法是跳转到登录页面用的
    @RequestMapping(value = "/loginPage")
    String loginPage(boolean isBack, HttpServletRequest request) {
        if (isBack){
            return "loginPage";
        }
        // 另一种登录页面
        SavedRequest savedRequest = WebUtils.getSavedRequest(request);// shiro保存拦截之前的请求对象
        if (savedRequest != null) {
            String queryString = savedRequest.getQueryString();// 获取参数字符串
            if ("/pages/back/merchant/addPre".equals(savedRequest.getRequestURI())) {// 证明此时shiro拦截的是客户端
                return "client-loginPage";
            }
        }
        return "client-loginPage";
    }

    @RequestMapping("/logoutApp")
    public String logout(){
        SecurityUtils.getSubject().logout();
        return "/loginPage";
    }

    @RequestMapping("/pages/back/home")
    public String goHome(boolean isBack,HttpServletRequest request) {
        if (isBack) {
            return "pages/back/home";// 跳转到后台管理界面
        } else {
            // 不是后台登录，就是前端登录
            SavedRequest savedRequest = WebUtils.getSavedRequest(request);// shiro保存拦截之前的请求对象
            if (savedRequest != null) { // 判断是否曾拦截
                String queryString = savedRequest.getQueryString();// 获取参数字符串,有被拦截的路径，就跳转回拦截之前的那个路径
                // 这个url就是前台发送的"/pages/back/merchant/addPre"
                return "redirect:" + savedRequest.getRequestURI() + "?" + queryString;
            }
            return "pages/back/client/my-info";// 应该跳转到客户端我的界面
        }
    }

    @RequestMapping("/register")
    public String register(){
        return "register";
    }

    @ResponseBody
    @RequestMapping("/reg")
    public Object reg(UserVO userVO){
        if (userVO!=null){
            boolean justNmae = userVO.getNickName() != null&&userVO.getNickName()!="";
            boolean justPwd = userVO.getPassword()!=null&&userVO.getPassword()!="";
            if (justNmae&&justPwd){
                int id = userService.addUser(userVO);
                if (id!=0){
                    return ResponseDTO.ok("添加成功");
                }
            }
        }
        return ResponseDTO.fail("添加失败");
    }

    // 这个方法是跳转到登录页面用的
    @RequestMapping("/pages/back/dashBoard")
    String dashBoard() {
        return "pages/back/dashboard/dashboard";// 应该跳转到客户端我的界面
    }

}
